package com.keyware.kd.service.impl.number;

import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.PropertiesLoaderUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.keyware.kd.context.KDExceptionHandler;
import com.keyware.kd.repository.entity.number.NumberBuilder;
import com.keyware.kd.repository.mybatis.itf.number.NumberBuilderMapper;
import com.keyware.kd.service.itf.number.NumberBuilderService;
import com.keyware.kd.utils.Constant;

@Service("numberBuilderService")
public class NumberBuilderServiceImpl implements NumberBuilderService{
	public static Logger logger = Logger.getLogger(NumberBuilderServiceImpl.class);
	@Autowired
	private NumberBuilderMapper numberBuilderMapper;
	
	//userNumber需要返回一个字符串，需要在此处转换一下
	@Override
	public String getUserNumber() {
		Integer userNumber = getNumber("user_code");
		NumberBuilder selectByPrimaryKey = numberBuilderMapper.selectByPrimaryKey("userCodePre");
		if(userNumber == 1){
			if(selectByPrimaryKey != null)
				return selectByPrimaryKey.getNumber()+"0001";
			else
				return Constant.RULE_NUMBER+"0001";
		}else{
			if(selectByPrimaryKey != null)
				return selectByPrimaryKey.getNumber()+getStringNumber(userNumber);
			else
				return Constant.RULE_NUMBER+getStringNumber(userNumber);
		}
	}
	
	//获取编号，如果确认业务增加之后，数据库的编码再增加1
	@Override
	public Integer updateNumber(String codeType) {
		NumberBuilder numberBuilder = numberBuilderMapper.selectByPrimaryKey(codeType);
		if(numberBuilder == null){
			NumberBuilder numberBuilder2 = new NumberBuilder();
			if("seq_code".equals(codeType)){      //项目管理默认是1，其他从2开始
				numberBuilder2.setNumber("2");
			}else{
				numberBuilder2.setNumber("1");
			}
			numberBuilder2.setNumberId(codeType);
			numberBuilderMapper.insertSelective(numberBuilder2);
			if("seq_code".equals(codeType)){
				return 2;
			}
			return 1;
		}else{
			Integer codeValue = Integer.valueOf(numberBuilder.getNumber())+1;
			numberBuilder.setNumber(codeValue.toString());
			numberBuilderMapper.updateByPrimaryKeySelective(numberBuilder);
			return Integer.valueOf(numberBuilder.getNumber());	
		}
	}

	@Override
	public Integer getNumber(String codeType) {
		NumberBuilder numberBuilder = numberBuilderMapper.selectByPrimaryKey(codeType);
		if(numberBuilder == null){
			if("seq_code".equals(codeType)){      //项目管理默认是1，其他从2开始
				return 2;
			}
			return 1;
		}else{
			return Integer.valueOf(numberBuilder.getNumber())+1;
		}
	}
	
	private String getStringNumber(Integer number) {
		String strNumber = number.toString();
		while(strNumber.length() < 4){
			strNumber = "0" + strNumber;
		}
		return strNumber;
	}

	@Override
	public boolean saveOrUpdateCustomerId(String customerId, String projectName) {
		if(StringUtils.hasText(projectName))
			projectName = projectName.toUpperCase();
		
		NumberBuilder numberBuilder = numberBuilderMapper.selectByPrimaryKey("customer_id_"+projectName);
		if(null == numberBuilder){   //如果不存在直接新增
			NumberBuilder numberBuilder2 = new NumberBuilder();
			numberBuilder2.setNumberId("customer_id_"+projectName);
			numberBuilder2.setNumber(customerId);
			numberBuilderMapper.insertSelective(numberBuilder2);
		}else{
			numberBuilder.setNumber(customerId);
			numberBuilderMapper.updateByPrimaryKeySelective(numberBuilder);
		}
		return true;
	}

	@Override
	public String getCustomerId() {
		NumberBuilder numberBuilder = numberBuilderMapper.selectByPrimaryKey("customer_id");
		if(numberBuilder != null){
			String customerId = numberBuilder.getNumber();
			return String.valueOf(customerId);
		}else{
			return "0";
		}
	}

	@Override
	public int deleteByPrimaryKey(String numberId) {
		return numberBuilderMapper.deleteByPrimaryKey(numberId);
	}

	@Override
	public int insert(NumberBuilder record) {
		return numberBuilderMapper.insert(record);
	}

	@Override
	public int insertSelective(NumberBuilder record) {
		return numberBuilderMapper.insertSelective(record);
	}

	@Override
	public NumberBuilder selectByPrimaryKey(String numberId) {
		return numberBuilderMapper.selectByPrimaryKey(numberId);
	}

	@Override
	public int updateByPrimaryKeySelective(NumberBuilder record) {
		return numberBuilderMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(NumberBuilder record) {
		return numberBuilderMapper.updateByPrimaryKey(record);
	}

	@Override
	public void saveConfigure(Integer number) {
		NumberBuilder selectByPrimaryKey = this.selectByPrimaryKey("configure");
		if(selectByPrimaryKey == null){
			NumberBuilder numberBuilder = new NumberBuilder();
			numberBuilder.setNumberId("configure");
			numberBuilder.setNumber(String.valueOf(number));
			this.insertSelective(numberBuilder);
		}else{
			selectByPrimaryKey.setNumber(String.valueOf(number));
			this.updateByPrimaryKeySelective(selectByPrimaryKey);
		}
	}

	@Override
	public Boolean checkNumberValue(String numberId, String trueValue) {
		NumberBuilder selectByPrimaryKey = this.selectByPrimaryKey(numberId);
		if(selectByPrimaryKey == null)
			return false;
		else {
			if(selectByPrimaryKey.getNumber().toString().equals(trueValue))
				return true;
			else
				return false;
		}
	}

	@Override
	public Boolean checkConfigure() {
		
		return this.checkNumberValue("configure","0");
	}

	@Override
	public List<NumberBuilder> selectByInfo(NumberBuilder record) {
		
		return numberBuilderMapper.selectByInfo(record);
	}

	@Override
	public Properties getProjectTypeParameter() {
		Properties properties = new Properties();
		String  numCode = getCustomerId();
			String url = numCode+".properties";
			try {
				properties = PropertiesLoaderUtils.loadAllProperties(url);
				@SuppressWarnings("rawtypes")
				Enumeration enum1 = properties.propertyNames();
				if(null == properties  || !enum1.hasMoreElements()){
					 url = "keyware.properties";
					 properties = PropertiesLoaderUtils.loadAllProperties(url);
				}
				return properties;
			} catch (IOException e) {
				logger.error(e);
				e.printStackTrace();
			}  
		return null;
	}

}
